package cn.pdx.elec.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.pdx.elec.domain.ElecUser;

public class LoginFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}
	//需要定义系统页面访问中可放行的链接
	List<String> list=new ArrayList<String>();
	public void init(FilterConfig arg0) throws ServletException {

		
		list.add("/index.jsp");
		list.add("/image.jsp");
		list.add("/system/elecLoginAction_home.do"); //登录跳转的链接必须放行
		
	}
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request=(HttpServletRequest) servletRequest;
		HttpServletResponse response=(HttpServletResponse) servletResponse;
		
		//获取页面中的访问链接
		String path=request.getServletPath();
		if(list!=null && list.contains(path))
		{
			//1.如果页面中获取的访问链接与定义的可放行的连接一致,则放行
			chain.doFilter(request, response);
			return;
		}
		//2.从session中获取当前登录的用户
		ElecUser elecUser=(ElecUser)request.getSession().getAttribute("globle_user");
		if(elecUser!=null)
		{
			//从session中获取的对象不为空,则放行
			chain.doFilter(request, response);
			return;
		}
		//如果不满足1和2条件，就回到登录页面
		response.sendRedirect(request.getContextPath()+"/");
	}

	

}
